using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.IO.IsolatedStorage;
using System.IO;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;

namespace ebookreader
{
    public static class Extensions
    {

        public static void SetPortraitOrientation(this GraphicsDeviceManager graphics)
        {
            graphics.SupportedOrientations = DisplayOrientation.Portrait;
            
            graphics.PreferredBackBufferHeight = 800;
            graphics.PreferredBackBufferWidth = 480;

            graphics.ApplyChanges();
        }

        public static void SetLandscapeLeftOrientation(this GraphicsDeviceManager graphics)
        {
            graphics.SupportedOrientations = DisplayOrientation.LandscapeLeft;
            graphics.PreferredBackBufferHeight = 480;
            graphics.PreferredBackBufferWidth = 800;

            graphics.ApplyChanges();
        }

        public static void SetLandscapeRightOrientation(this GraphicsDeviceManager graphics)
        {
            graphics.SupportedOrientations = DisplayOrientation.LandscapeRight;
            graphics.PreferredBackBufferHeight = 480;
            graphics.PreferredBackBufferWidth = 800;

            graphics.ApplyChanges();
        }


        public static string readFromFile(this String str, String path)
        {
            IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFileStream stream;
            try
            {
                stream = store.OpenFile(path, FileMode.Open, FileAccess.ReadWrite);
            }
            catch (Exception)
            {
                store.Dispose();
                return "";
            }
            var sr = new StreamReader(stream);

            return sr.ReadToEnd();
        }

        public static Matrix ScreenTransformMatrix(this Game game, GraphicsDeviceManager graphics)
        {
            //return Matrix.CreateScale(game.GraphicsDevice.Viewport.Width / DEFAULT_SCREEN_WIDTH, game.GraphicsDevice.Viewport.Height / DEFAULT_SCREEN_HEIGHT, 1.0f);
            //if (game.Window.CurrentOrientation == DisplayOrientation.Portrait || game.Window.CurrentOrientation==DisplayOrientation.Default)
            if (graphics.SupportedOrientations == DisplayOrientation.Portrait || graphics.SupportedOrientations == DisplayOrientation.Default)
                //if (ScreenPosition.Position == ScreenPositions.Portrait)
                return Matrix.CreateScale(game.GraphicsDevice.Viewport.Width / 480, game.GraphicsDevice.Viewport.Height / 800, 1.0f);
            else
                return Matrix.CreateScale(game.GraphicsDevice.Viewport.Width / 800, game.GraphicsDevice.Viewport.Height / 480, 1.0f);
        }



        //public static void DrawString(this SpriteBatch self, SpriteFont font, String text, Vector2 position, Color color, float justif_width, float just_tollerance)
        //{
        //    var length = font.MeasureString(text).X;
        //    if (length * just_tollerance < justif_width)
        //    {
        //        self.DrawString(font, text, position, color);
        //        return;
        //    }
        //    var num_spaces = text.Split(new char[] { ' ' }).Length-1;
        //    var inc = (justif_width - length) / num_spaces;
        //    var prev_spacing = font.Spacing;
        //    font.Spacing += inc;
        //    self.DrawString(font, text, position, color);
        //    font.Spacing = prev_spacing;
        //}
    }
}
